package com.club.football.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.club.football.common.UserHolder;
import com.club.football.dto.EventsDetailDto;
import com.club.football.dto.PageDto;
import com.club.football.dto.PlayerSaveDto;
import com.club.football.entity.Events;
import com.club.football.entity.EventsDetail;
import com.club.football.entity.Player;
import com.club.football.mapper.PlayerMapper;
import com.club.football.req.PlayerPageReq;
import com.club.football.service.EventsDetailService;
import com.club.football.service.EventsService;
import com.club.football.service.PlayerService;
import com.club.football.utils.BeanUtils;
import com.club.football.utils.FileUtils;
import com.club.football.utils.R;
import com.club.football.vo.PlayerPageVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.club.football.common.CommonConstants.*;

/**
 * 球员服务实现
 *
 * @author 1
 * @date 2024/01/09
 */
@Service
public class PlayerServiceImpl extends ServiceImpl<PlayerMapper, Player> implements PlayerService {
    @Resource
    private EventsDetailService eventsDetailService;
    @Resource
    private EventsService eventsService;


    @Override
    public R<String> playerSave(PlayerSaveDto saveDto) {
        Player player = BeanUtils.copier(saveDto, new Player());
        Long userId = UserHolder.getUser().getUserId();
        player.setUpdateBy(userId);
        String playerImg = saveDto.getPlayerImg();
        if (player.getPlayerId() != null) {
            if (StringUtils.isBlank(playerImg)) {
                player.setPlayerImg(getById(saveDto.getPlayerId()).getPlayerImg());
            }
            updateById(player);
            return R.ok(EDIT_OK_MSG);
        }
        player.setCreateBy(userId);
        if (StringUtils.isNotBlank(playerImg)) {
            player.setPlayerImg(FileUtils.getImageUrl(DEFAULT_IMAGE));
        }
        save(player);
        return R.ok(SAVE_OK_MSG);
    }

    @Override
    public R<Page<PlayerPageVo>> playerPage(PageDto<Player> pageDto, PlayerPageReq playerPageReq) {
        lambdaQuery().like(StringUtils.isNotBlank(playerPageReq.getName()), Player::getName, playerPageReq.getName())
                .eq(playerPageReq.getTeamId() != null, Player::getTeamId, playerPageReq.getTeamId()).page(pageDto);
        List<Player> records = pageDto.getRecords();
        Page<PlayerPageVo> playerPage = new Page<>();
        if (CollUtil.isNotEmpty(records)) {
            List<PlayerPageVo> playerPageVos = new ArrayList<>();
            for (Player player : records) {
                extracted(player, playerPageVos);
            }
            playerPage.setTotal(pageDto.getTotal());
            playerPage.setRecords(playerPageVos);
        }
        return R.ok(playerPage, GET_INFO_OK);
    }

    private void extracted(Player player, List<PlayerPageVo> playerPageVos) {
        List<EventsDetailDto> detailDtoList = new ArrayList<>();
        PlayerPageVo playerPageVo = BeanUtils.copier(player, new PlayerPageVo());
        playerPageVo.setPlayerImg(FileUtils.getUrl(playerPageVo.getPlayerImg()));
        List<EventsDetail> eventsDetailList = eventsDetailService.lambdaQuery().eq(EventsDetail::getPlayerId, player.getPlayerId()).list();
        Map<Long, List<EventsDetail>> mapByEventsId = eventsDetailList.stream().collect(Collectors.groupingBy(EventsDetail::getEventsId));
        mapByEventsId.forEach((k, v) -> {
            Events events = eventsService.getById(k);
            EventsDetailDto detailDto = BeanUtils.copier(events, new EventsDetailDto());
            detailDto.setEventsName(events.getName());
            double score = v.stream().mapToDouble(EventsDetail::getScore).sum();
            double goals = v.stream().mapToDouble(EventsDetail::getGoals).sum();
            detailDto.setScore(score);
            detailDto.setGoals(goals);
            detailDtoList.add(detailDto);
        });
        playerPageVo.setDetailDtoList(detailDtoList);
        playerPageVos.add(playerPageVo);
    }
}
